//
//  BSTagViewController.m
//  BaiSi
//
//  Created by Sycamore on 16/4/28.
//  Copyright © 2016年 Sycamore. All rights reserved.
//

#import "BSTagViewController.h"
#import "BSTagCell.h"
#import <AFNetworking/AFNetworking.h>
#import "BSTagItem.h"
#import <MJExtension/MJExtension.h>
#import <SVProgressHUD/SVProgressHUD.h>

@interface BSTagViewController ()

@property (nonatomic,strong) NSArray *items;

@end

@implementation BSTagViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"推荐标签";
    self.tableView.backgroundColor = BSTintColor;
    
    //ios6，分割线有一段距离
    //ios7,分割线取消距屏幕左边的距离
    //ios8,分割线又设置了一段距离
//    self.tableView.separatorInset = UIEdgeInsetsZero;
    //获取网络数据
    if (_items == nil) {
        [self loadData];

    }
    
}



- (void)loadData
{
    [SVProgressHUD showWithStatus:@"正在加载中"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSDictionary *parameter = @{
                                @"a" : @"tag_recommend",
                                @"action" : @"sub",
                                @"c" : @"topic"
                                };
    
    [manager GET:BSBaseURL parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary*  _Nullable responseObject) {
//        NSLog(@"%@",responseObject);
        //字典数组转模型数组
        _items = [BSTagItem mj_objectArrayWithKeyValuesArray:responseObject];
        
        //刷新数据
        [self.tableView reloadData];
        [SVProgressHUD dismiss];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
    }];
    
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [SVProgressHUD dismiss];
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return _items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"cell";
    BSTagCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell =  [BSTagCell tagCell];
    }
    
    cell.item = _items[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //ios8以后，增加了constraintMargin，默认距屏幕边缘有一段距离，设置这个属性就可以取消这段距离
    //但是ios7以前的并不适用
//    cell.layoutMargins = UIEdgeInsetsZero;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 61;
}

@end
